package hesso.smartdraw.ch.guielements;

import hesso.smartdraw.ch.DrawWallScreen;
import hesso.smartdraw.ch.model.Color;

import java.util.ArrayList;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

public class LevelAddHandler extends Sprite {
	
	private Color color;
	
	private ArrayList<Level> levels;
	
	private TextureRegion levelTexture;
	
	private Scene scene;
	
	private boolean isSub;
	
	private LevelRemoveHandler removeButton;
	
	private DrawWallScreen dw;

	public LevelAddHandler(float pX, float pY, ArrayList<Level> levels, 
				boolean isSub, Scene scene, TextureRegion level, TextureRegion plus, TextureRegion minus,
				DrawWallScreen dw) {	
		
		super(pX, pY, plus);
		this.dw = dw;
		this.levels = levels;
		this.isSub = isSub;
		this.scene = scene;
		this.levelTexture = level;
		//setColor(Color.BLUE);
		
		removeButton = new LevelRemoveHandler(this.getX() + this.getWidth(), this.getY(),
				levels, isSub, this.scene, this, minus, dw);
		
		scene.attachChild(removeButton);
		scene.registerTouchArea(removeButton);
	}
	
	private void setColor(Color c) {		
		this.color = c;
		super.setColor(c.getRed(), c.getGreen(), c.getBlue());
	}
	
	public void addLevel() {
		
		//if(levels.size()-1 <= 5) {
			// TODO do other stuff lol
			int levelNumber = dw.getNewLevel(!isSub);
			Level tempLevel = new Level(this.getX(), this.getY(), levelTexture, levelNumber, dw, false);
			dw.addLevel(levelNumber);
			levels.add(tempLevel);
			scene.attachChild(tempLevel);
			scene.registerTouchArea(tempLevel);
			
			if(!isSub) {

				this.setPosition(this.getX(), this.getY() - this.getHeight());
				removeButton.setPosition(this.getX() + this.getWidth(), this.getY());
			} else {

				this.setPosition(this.getX(), this.getY() + this.getHeight());
				removeButton.setPosition(this.getX() + this.getWidth(), this.getY());
			}
	//	}
		
	}
	
	@Override
    public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
            switch(pSceneTouchEvent.getAction()) {
                    case TouchEvent.ACTION_DOWN:
                    	
                            break;

                    case TouchEvent.ACTION_MOVE:

                            break;
                    case TouchEvent.ACTION_UP:
                    	this.addLevel();
                            break;
            }
            return true;
    }

}
